package com.iwish.server.model;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.iwish.server.storage.model.Obj;
import com.iwish.server.util.Base64Util;
import com.iwish.server.util.RequestCode;
import com.iwish.server.util.Separator;

public class RegisterWithSinaAccountService extends ServiceTemplate{
	
	Logger logger = Logger.getLogger(RegisterWithSinaAccountService.class);
	
	private String sina_account;
	private String img;
	private String nickname;
	private boolean gender;
	private String date_of_birth;
	
	public RegisterWithSinaAccountService()
	{
		
	}
	public RegisterWithSinaAccountService(String sina_account
			, String img
			, String nickname
			, boolean gender
			, String date_of_birth)
	{
		this.sina_account = sina_account;
		this.img = img;
		this.nickname = nickname;
		this.gender = gender;
		this.date_of_birth = date_of_birth;
	}
	
	public boolean register(IoSession session)
	{
		Obj.User.Builder user = Obj.User.newBuilder();
		int index = 0;
		String uid = "u." + sina_account;
		user.setUid(uid);
		user.setUname(sina_account);
		user.setNick(nickname);
		user.setGender(gender);
		//user.setBirth(date_of_birth);
		//user.setImages(index,img);
		
		set(Obj.User.class, uid,user.build());
		
		String result = Base64Util.encode(RequestCode.RESULT_REGISTER_SINA_ACCOUNT.getBytes())
				+ Separator.F_SEPARATOR
				+ Base64Util.encode(RequestCode.SUCCESS.getBytes())
				+ Separator.F_SEPARATOR
				//empty fail reason
				+ Separator.F_SEPARATOR
				;
		session.write(result);
		return true;
	}
}
